package com.jeethink.web.controller.gs;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeethink.common.config.JeeThinkConfig;
import com.jeethink.gs.domain.GsTask;
import com.jeethink.gs.domain.GsTaskDTO2;
import com.jeethink.gs.domain.dto.GsFormTemplateDTO;
import com.jeethink.gs.service.GsTaskService;


import com.jeethink.web.core.ExcelUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.jeethink.common.annotation.Log;
import com.jeethink.common.enums.BusinessType;
import com.jeethink.common.core.controller.BaseController;
import com.jeethink.common.core.domain.AjaxResult;
import com.jeethink.common.utils.poi.ExcelUtil;
import com.jeethink.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * 任务Controller
 * 
 * @author jeethink
 * @date 2022-08-08
 */
@Controller
@RequestMapping("/gs/task")
public class GsTaskController extends BaseController
{
    private String prefix = "gs/task";


    @Autowired
    private GsTaskService gsTaskService;

    @RequiresPermissions("gs:task:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/task";
    }

    /**
     * 查询任务列表
     */
    @RequiresPermissions("system:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GsTask gsTask)
    {
        startPage();
        List<GsTask> list = gsTaskService.selectGsTaskList(gsTask);
        return getDataTable(list);
    }

    /**
     * 导出任务列表
     */
    @RequiresPermissions("system:task:export")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GsTask gsTask)
    {
        List<GsTask> list = gsTaskService.selectGsTaskList(gsTask);
        ExcelUtil<GsTask> util = new ExcelUtil<GsTask>(GsTask.class);
        return util.exportExcel(list, "task");
    }

    /**
     * 新增任务
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @GetMapping("/template")
    public String template()
    {
        return prefix + "/template";
    }

    @GetMapping("/task")
    public String task1()
    {
        return prefix + "/task";
    }

    @GetMapping("/edit")
    public String edit()
    {
        return prefix + "/edit";
    }

    /**
     * 新增保存任务
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody JSONObject data)
    {
        String json1=JSONObject.toJSONString(data.getJSONObject("gsTask"));
        GsTaskDTO2 gsTask=JSON.parseObject(json1,GsTaskDTO2.class);
        return toAjax(gsTaskService.insertGsTask(gsTask));
    }

//    /**
//     * 修改任务
//     */
//    @GetMapping("/edit/{id}")
//    public String edit(@PathVariable("id") String id, ModelMap mmap)
//    {
//        GsTask gsTask = gsTaskService.selectGsTaskById(id);
//        mmap.put("gsTask", gsTask);
//        return prefix + "/edit";
//    }

    /**
     * 修改保存任务
     */
    @RequiresPermissions("system:task:edit")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GsTask gsTask)
    {
        return toAjax(gsTaskService.updateGsTask(gsTask));
    }

    /**
     * 删除任务
     */
    @RequiresPermissions("system:task:remove")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gsTaskService.deleteGsTaskByIds(ids));
    }

    /**
     * 获取任务发布列表
     * @author Hyunjin
     * @date 14:56 2022/8/9
     * @param taskName
     * @return com.jeethink.common.core.page.TableDataInfo
     **/
    @PostMapping("/getTaskPublishing")
    @ResponseBody
    public TableDataInfo getTaskPublishing(@RequestParam(value = "taskName",defaultValue = "") String taskName){
        startPage();
        return getDataTable(gsTaskService.getTaskPublishing(taskName));
    }

    /**
     * 获取任务填报列表
     * @author Hyunjin
     * @date 14:56 2022/8/9
     * @param taskType
     * @param taskStartTime
     * @return com.jeethink.common.core.page.TableDataInfo
     **/
    @GetMapping("/getTaskWrite")
    @ResponseBody
    public TableDataInfo getTaskWrite(@RequestParam(value = "taskType",defaultValue = "") String taskType,
                                      @RequestParam(value = "taskStartTime",defaultValue = "") String taskStartTime){
        startPage();
        return getDataTable(gsTaskService.getTaskWrite(taskType,taskStartTime));
    }

    /**
     * 获取任务填报审核列表
     * @author Hyunjin
     * @date 14:58 2022/8/9
     * @param taskType
     * @param taskStartTime
     * @return com.jeethink.common.core.page.TableDataInfo
     **/
    @GetMapping("/getTaskWriteExamine")
    @ResponseBody
    public TableDataInfo getTaskWriteExamine(@RequestParam(value = "taskType",defaultValue = "") String taskType,
                                             @RequestParam(value = "taskStartTime",defaultValue = "") String taskStartTime){
        startPage();
        return getDataTable(gsTaskService.getTaskWriteExamine(taskType,taskStartTime));
    }

    /**
     * 查看任务详情
     * @author Hyunjin
     * @date 16:46 2022/8/9
     * @param id
     * @return com.jeethink.common.core.domain.AjaxResult
     **/
    @GetMapping("/selectGsTaskById")
    @ResponseBody
    public AjaxResult selectGsTaskById(@RequestParam(value = "id") String id){
        return gsTaskService.selectGsTaskById(id);
    }

    /**
     * 修改任务状态
     * @author Hyunjin
     * @date 17:59 2022/8/9
     * @param taskState
     * @param id
     * @return com.jeethink.common.core.domain.AjaxResult
     **/
    @GetMapping("/updateTaskByState")
    @ResponseBody
    public AjaxResult updateTaskByState(@RequestParam(value = "taskState") String taskState,
                                        @RequestParam(value = "id") String id){
        return gsTaskService.updateTaskByState(taskState,id);
    }

    @PostMapping("/getSourceData")
    @ResponseBody
    public AjaxResult getSourceData(@RequestParam String pathname) throws Exception {
//        // 上传文件路径
//        String filePath = JeeThinkConfig.getUploadPath();
//        for(int i = 0; i < 3; i++){
//            pathname = pathname.substring(pathname.indexOf("/")+1 );
//        }
//        ListenerDataSheet listener = new ListenerDataSheet(new DynamicHeaderDataConvert<>(SubjectScore.class));
//        EasyExcel.read("DynamicHeaderReader.xlsx", listener)
//                .doReadAll();
//        listener.getResult();
//        System.out.println("===============" + JSON.toJSONString(row));
//        ExcelUtils excelUtils=new ExcelUtils();
//        return AjaxResult.success( excelUtils.getExcelData(filePath+"/"+pathname));
        // 上传文件路径
        String filePath = JeeThinkConfig.getUploadPath();
        for(int i = 0; i < 3; i++){
            pathname = pathname.substring(pathname.indexOf("/")+1 );
        }
        ExcelUtils excelUtils=new ExcelUtils();
        return AjaxResult.success( excelUtils.readExcelWithoutTitle(filePath+"/"+pathname));
    }


    @PostMapping("/excelData")
    @ResponseBody
    public Object ExcelData(MultipartFile file, HttpServletRequest request) {
        List<String[]> list= ExcelUtils.readExcel(file,0,0);
        String[] nameList=list.get(0);
        for(int i=1;i<list.size();i++){
            String[] name=list.get(i);
            for (int i1=0;i1<name.length;i1++){
                if (!name[i1].equals("")){
                    nameList[i1]= nameList[i1]+","+ name[i1];
                }
            }
        }
        List<GsFormTemplateDTO> fromList=new ArrayList<>();
        for (int i=0;i<nameList.length;i++){
            GsFormTemplateDTO gsFormTemplateDTO=new GsFormTemplateDTO();
            gsFormTemplateDTO.setName(nameList[i]);
            gsFormTemplateDTO.setBinding("无");
            gsFormTemplateDTO.setInputType("文本输入框");
            fromList.add(gsFormTemplateDTO);
        }
        System.out.println("--------"+JSON.toJSONString(fromList));
        request.getSession().setAttribute("formList",fromList);
        return fromList;
    }

    @GetMapping("/customs")
    public String custom(HttpServletRequest request,ModelMap map)
    {

        map.put("formList",request.getSession().getAttribute("formList"));
        System.out.println(JSON.toJSONString(request.getSession().getAttribute("formList")));
        return prefix + "/customs";
    }
}
